package com.halflife.comeElectricity.member.rider;

import com.halflife.comeElectricity.basic.datadic.Discount;
import com.halflife.comeElectricity.basic.datadic.DiscountRepository;
import com.halflife.framework.converter.MapHandler;
import com.halflife.framework.domain.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class DiscountLogAdminController {

    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Resource
    private DiscountLogRepository discountLogRepository;
    @Resource
    private RiderRepository riderRepository;
    @Resource
    private DiscountRepository discountRepository;



    @RequestMapping("/discountLog/list")
    public String gotoList(@RequestParam(required = false) HashMap<String,Object> queryParam, Page page, HttpServletResponse response,
                           HttpServletRequest request, Model model) {
        MapHandler.handleMap(queryParam);
        if (page == null) {
            page = new Page();
        }
        page.setQueryParam(queryParam);
        int totalCount = discountLogRepository.selectCount(queryParam);
        List<DiscountLog> list = discountLogRepository.selectListByPage(queryParam);
        page.setTotalCount(totalCount);
        page.setMode(model);
        model.addAttribute("list",list);
        model.addAttribute("queryParam",queryParam);
        model.addAttribute("riderId",queryParam.get("riderId"));
        return "discountLog/list";
    }

    @RequestMapping("/discountLog/operate")
    public String gotoOperate(Integer riderId,Integer id,HttpServletResponse response,
                               HttpServletRequest request, Model model) {
        DiscountLog discountLog = new DiscountLog();
        if(id!=null){
            discountLog = discountLogRepository.selectByPrimaryKey(id);
        }else{
            Rider rider = riderRepository.selectByPrimaryKey(riderId);
            if (rider == null) {
                throw new RuntimeException("找不到骑手,请联系管理员");
            }
            discountLog.setRiderId(riderId);
            discountLog.setPhone(rider.getPhone());
            discountLog.setRiderName(rider.getName());
        }
        model.addAttribute("discountLog",discountLog);
        return "discountLog/operate";
    }

    @PostMapping("/discountLog/save")
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseEntity save(DiscountLog discountLog, HttpServletRequest request,
                              HttpServletResponse response, Model model) {
        try {
            if(discountLog.getId()!=null){
                discountLogRepository.updateByPrimaryKey(discountLog);
            }else {
                discountLog.init();
                Integer a = discountLogRepository.insert(discountLog);
            }
        } catch (Exception ex) {
            log.error(ex.toString(),ex);
            return new ResponseEntity(1, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity(0, HttpStatus.OK);
    }
    @GetMapping("/discountLog/handle")
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseEntity handle(Integer discountId, Integer riderId,HttpServletRequest request,
                              HttpServletResponse response, Model model) {
        try {
            Discount discount = discountRepository.selectByPrimaryKey(discountId);
            if (discount == null) {
                throw new RuntimeException("找不到优惠券对象,ID:"+discountId);
            }
            Rider rider = riderRepository.selectByPrimaryKey(riderId);
            if (rider == null) {
                throw new RuntimeException("找不到骑手对象,ID:"+riderId);
            }
            DiscountLog log = new DiscountLog();
            log.setRiderName(rider.getName());
            log.setRiderId(rider.getId());
            log.setPhone(rider.getPhone());
            log.setDiscountId(discount.getId());
            log.setCreateDate(new Date().getTime());
            log.setDiscountMoney(discount.getValue());
            log.setCatagory(discount.getCatagory());
            log.init();
            discountLogRepository.insert(log);
        } catch (Exception ex) {
            log.error(ex.toString(),ex);
            return new ResponseEntity(1, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity(0, HttpStatus.OK);
    }

    @DeleteMapping("/discountLog/delete")
    public ResponseEntity setRiderState(Integer id, Integer state) {
        try {
            DiscountLog discountLog = new DiscountLog();
            discountLog.setId(id);
            discountLog.setValid(Boolean.FALSE);
            discountLogRepository.updateByPrimaryKeySelective(discountLog);
        } catch (Exception ex) {
            log.error(ex.toString(),ex);
            return new ResponseEntity(1, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity(0, HttpStatus.OK);
    }
    @RequestMapping("/discountLog/setEndDate")
    public ResponseEntity setEndDate(Integer id, String endDate) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date endDates = sdf.parse(endDate);
            DiscountLog discountLog = new DiscountLog();
            discountLog.setId(id);
            discountLog.setEndDate(endDates.getTime());
            discountLogRepository.updateByPrimaryKeySelective(discountLog);
        } catch (Exception ex) {
            log.error(ex.toString(),ex);
            return new ResponseEntity(1, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity(0, HttpStatus.OK);
    }
}
